home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / inevow.py < prev    next >
Text File  |  2008-01-31  |  20KB  |  617 lines

  1. # -*- test-case-name: nevow -*-
  2. # Copyright (c) 2004-2008 Divmod.
  3. # See LICENSE for details.
  4.  
  5. """
  6. Nevow interface definitions.
  7. """
  8.  
  9. from zope.interface import Interface, Attribute
  10.  
  11. class IQ(Interface):
  12.     """Interface for querying. Adapters implement this for objects which may
  13.     appear in the stan DOM to allow introspecting the DOM and finding nodes
  14.     with certain qualities.
  15.     """
  16.     def patternGenerator(pattern, default=None):
  17.         """Returns a pseudo-Tag which will generate clones of matching
  18.         pattern tags forever, looping around to the beginning when running
  19.         out of unique matches.
  20.  
  21.         If no matches are found, and default is None, raise an exception,
  22.         otherwise, generate clones of default forever.
  23.  
  24.         You can use the normal stan syntax on the return value.
  25.  
  26.         Useful to find repeating pattern elements. Example rendering
  27.         function:
  28.  
  29.         >>> def simpleSequence(context, data):
  30.         ...   pattern = IQ(context).patternGenerator('item')
  31.         ...   return [pattern(data=element) for element in data]
  32.         """
  33.  
  34.     def allPatterns(pattern):
  35.         """Return a list of all matching pattern tags, cloned.
  36.  
  37.         Useful if you just want to insert them in the output in one
  38.         place.
  39.  
  40.         E.g. the sequence renderer's header and footer are found with this.
  41.         """
  42.  
  43.     def onePattern(pattern):
  44.         """Return a single matching pattern, cloned.
  45.         If there is more than one matching pattern or no matching patterns,
  46.         raise an exception.
  47.  
  48.         Useful in the case where you want to locate one and only one
  49.         sub-tag and do something with it.
  50.         """
  51.  
  52.     def keyed(key):
  53.         """Locate the node with the key 'key', clone it, call fillSlots(key, clone)
  54.         and return the clone.
  55.  
  56.         This method lets you effectively locate and mutate a node in the DOM.
  57.         It is useful for setting the data special on a specific node, and also for
  58.         calling fillSlots on a specific node, as well as other node-mutation operations
  59.         such as setting a handler or assigning a class or id to a node.
  60.         """
  61.  
  62.  
  63. class IResource(Interface):
  64.     def locateChild(ctx, segments):
  65.         """
  66.         Locate another object which can be adapted to IResource.
  67.  
  68.         @param ctx: The context object for the request being responded to.
  69.         @param segments: A tuple of strings giving the remaining query segments
  70.             to resolve into an IResource.
  71.  
  72.         @return: A two-tuple of an L{IResource} provider and a tuple giving the
  73.             query segments which remain to be processed.  A L{Deferred} which
  74.             is called back with such a two-tuple may also be returned.
  75.         """
  76.  
  77.     def renderHTTP(ctx):
  78.         """Render a request
  79.         """
  80.  
  81.  
  82. class IRenderer(Interface):
  83.     """Things implementing this interface are serialized by calling their
  84.     'rend' method.
  85.     """
  86.     def rend(ctx, data):
  87.         """Turn this instance into stan suitable for displaying it in a web page.
  88.         """
  89.  
  90.  
  91. class IRendererFactory(Interface):
  92.     """A renderer factory is capable of taking a renderer directive (a string)
  93.     and returning a callable which when called, will render a portion of DOM.
  94.     """
  95.     def renderer(context, name):
  96.         """Given a context object and a name, return a callable which responds
  97.         to the signature (context, data) or (data) and returns an object which
  98.         is flattenable.
  99.         """
  100.  
  101.  
  102. class IMacroFactory(Interface):
  103.     """A macro factory is capable of taking a macro directive (a string)
  104.     and returning a callable which when called, will replace the portion
  105.     of the DOM upon which the macro was placed with some different
  106.     DOM.
  107.     """
  108.     def macro(context, name):
  109.         """Given a context object and a name, return a callable which responds
  110.         to the signature (context, *parameters) and returns an object which
  111.         is flattenable.
  112.         """
  113.  
  114.  
  115. class IData(Interface):
  116.     """Any python object to be used as model data to be passed
  117.     to view functions. Used for marking the context stack only.
  118.  
  119.     ANY python object is said to implement IData.
  120.     """
  121.  
  122.  
  123. class IGettable(Interface):
  124.     def get(context):
  125.         """Return the data
  126.  
  127.         Return any object
  128.         """
  129.  
  130.  
  131. class ISettable(Interface):
  132.     def set(context, data):
  133.         """Set the data
  134.  
  135.         This might be removed.
  136.         """
  137.  
  138.  
  139. class IContainer(Interface):
  140.     def child(context, name):
  141.         """Return a conceptual child; an attribute, or a key,
  142.         or the result of a function call.
  143.  
  144.         Returns any object; the result may be adapted to IGettable
  145.         if possible.
  146.  
  147.         Return None if the adaptee does not have a child with the
  148.         given name.
  149.  
  150.         TODO: Maybe returning None is bad, and .child should just
  151.         raise whatever exception is natural
  152.         """
  153.  
  154. class IComponentized(Interface):
  155.     """I am a mixin to allow you to be adapted in various ways persistently.
  156.  
  157.     I define a list of persistent adapters.  This is to allow adapter classes
  158.     to store system-specific state, and initialized on demand.  The
  159.     getComponent method implements this.  You must also register adapters for
  160.     this class for the interfaces that you wish to pass to getComponent.
  161.  
  162.     Many other classes and utilities listed here are present in Zope3; this one
  163.     is specific to Twisted.
  164.     """
  165.  
  166.     def setComponent(interfaceClass, component):
  167.         """
  168.         Add a component to me for the indicated interface.
  169.         """
  170.  
  171.     def addComponent(component, ignoreClass=0, registry=None):
  172.         """
  173.         Add a component to me, for all appropriate interfaces.
  174.  
  175.         In order to determine which interfaces are appropriate, the component's
  176.         provided interfaces will be scanned.
  177.  
  178.         If the argument 'ignoreClass' is True, then all interfaces are
  179.         considered appropriate.
  180.  
  181.         Otherwise, an 'appropriate' interface is one for which its class has
  182.         been registered as an adapter for my class according to the rules of
  183.         getComponent.
  184.  
  185.         @return: the list of appropriate interfaces
  186.         """
  187.  
  188.     def getComponent(interface, registry=None, default=None):
  189.         """Create or retrieve an adapter for the given interface.
  190.  
  191.         If such an adapter has already been created, retrieve it from the cache
  192.         that this instance keeps of all its adapters.  Adapters created through
  193.         this mechanism may safely store system-specific state.
  194.  
  195.         If you want to register an adapter that will be created through
  196.         getComponent, but you don't require (or don't want) your adapter to be
  197.         cached and kept alive for the lifetime of this Componentized object,
  198.         set the attribute 'temporaryAdapter' to True on your adapter class.
  199.  
  200.         If you want to automatically register an adapter for all appropriate
  201.         interfaces (with addComponent), set the attribute 'multiComponent' to
  202.         True on your adapter class.
  203.         """
  204.  
  205.     def unsetComponent(interfaceClass):
  206.         """Remove my component specified by the given interface class."""
  207.  
  208.     def removeComponent(component):
  209.         """
  210.         Remove the given component from me entirely, for all interfaces for which
  211.         it has been registered.
  212.  
  213.         @return: a list of the interfaces that were removed.
  214.         """
  215.  
  216. class ISession(IComponentized):
  217.     """A web session
  218.  
  219.     You can locate a Session object to represent a unique web session using
  220.     ISession(ctx). This default session implementation uses cookies to
  221.     store a session identifier in the user's browser.
  222.  
  223.     uid: Session uid
  224.  
  225.     TODO: Need better docs; what's a session and why and how do you use it
  226.     """
  227.  
  228.     def setLifetime(lifetime):
  229.         """Set the approximate lifetime of this session, in seconds.
  230.  
  231.         This is highly imprecise, but it allows you to set some general
  232.         parameters about when this session will expire.  A callback will be
  233.         scheduled each 'lifetime' seconds, and if I have not been 'touch()'ed
  234.         in half a lifetime, I will be immediately expired.
  235.  
  236.         If you need to change the lifetime of all the sessions change sessionsLifeTime
  237.         attribute in class guard.SessionWrapper
  238.         """
  239.  
  240.     def notifyOnExpire(callback):
  241.         """Call this callback when the session expires or logs out.
  242.         """
  243.  
  244.     def expire():
  245.         """Expire/logout of the session.
  246.         """
  247.  
  248.     def touch():
  249.         """Refresh the session
  250.         """
  251.  
  252. class IGuardSession(ISession):
  253.     """ A web session base interface
  254.     Needed for guard to do its dirty job
  255.  
  256.     guard: SessionWrapper object
  257.     """
  258.     def portalLogout(port):
  259.         """Logout from portal port
  260.         """
  261.  
  262. class IRequest(IComponentized):
  263.     """A HTTP request.
  264.  
  265.     Subclasses should override the process() method to determine how
  266.     the request will be processed.
  267.  
  268.     @ivar method: The HTTP method that was used.
  269.     @ivar uri: The full URI that was requested (includes arguments).
  270.     @ivar path: The path only (arguments not included).
  271.     @ivar args: All of the arguments, including URL and POST arguments.
  272.     @type args: A mapping of strings (the argument names) to lists of values.
  273.                 i.e., ?foo=bar&foo=baz&quux=spam results in
  274.                 {'foo': ['bar', 'baz'], 'quux': ['spam']}.
  275.     @ivar received_headers: All received headers
  276.     """
  277.     # Methods for received request
  278.     def getHeader(key):
  279.         """Get a header that was sent from the network.
  280.  
  281.         Return C{None} if the header is not present.
  282.         """
  283.  
  284.  
  285.     def getCookie(key):
  286.         """Get a cookie that was sent from the network.
  287.         """
  288.  
  289.  
  290.     def getAllHeaders():
  291.         """Return dictionary of all headers the request received."""
  292.  
  293.     def getRequestHostname():
  294.         """Get the hostname that the user passed in to the request.
  295.  
  296.         This will either use the Host: header (if it is available) or the
  297.         host we are listening on if the header is unavailable.
  298.         """
  299.  
  300.     def getHost():
  301.         """Get my originally requesting transport's host.
  302.  
  303.         Don't rely on the 'transport' attribute, since Request objects may be
  304.         copied remotely.  For information on this method's return value, see
  305.         twisted.internet.tcp.Port.
  306.         """
  307.  
  308.     def getClientIP():
  309.         pass
  310.  
  311.     def getClient():
  312.         pass
  313.  
  314.     def getUser():
  315.         pass
  316.  
  317.     def getPassword():
  318.         pass
  319.  
  320.     def isSecure():
  321.         pass
  322.  
  323.     def getSession(sessionInterface = None):
  324.         pass
  325.  
  326.     def URLPath():
  327.         pass
  328.  
  329.     def prePathURL():
  330.         pass
  331.  
  332.     def rememberRootURL():
  333.         """
  334.         Remember the currently-processed part of the URL for later
  335.         recalling.
  336.         """
  337.  
  338.     def getRootURL():
  339.         """
  340.         Get a previously-remembered URL.
  341.         """
  342.  
  343.     # Methods for outgoing request
  344.     def finish():
  345.         """We are finished writing data."""
  346.  
  347.     def write(data):
  348.         """
  349.         Write some data as a result of an HTTP request.  The first
  350.         time this is called, it writes out response data.
  351.         """
  352.  
  353.     def addCookie(k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
  354.         """Set an outgoing HTTP cookie.
  355.  
  356.         In general, you should consider using sessions instead of cookies, see
  357.         twisted.web.server.Request.getSession and the
  358.         twisted.web.server.Session class for details.
  359.         """
  360.  
  361.     def setResponseCode(code, message=None):
  362.         """Set the HTTP response code.
  363.         """
  364.  
  365.     def setHeader(k, v):
  366.         """Set an outgoing HTTP header.
  367.         """
  368.  
  369.     def redirect(url):
  370.         """Utility function that does a redirect.
  371.  
  372.         The request should have finish() called after this.
  373.         """
  374.  
  375.     def setLastModified(when):
  376.         """Set the X{Last-Modified} time for the response to this request.
  377.  
  378.         If I am called more than once, I ignore attempts to set
  379.         Last-Modified earlier, only replacing the Last-Modified time
  380.         if it is to a later value.
  381.  
  382.         If I am a conditional request, I may modify my response code
  383.         to L{NOT_MODIFIED} if appropriate for the time given.
  384.  
  385.         @param when: The last time the resource being returned was
  386.             modified, in seconds since the epoch.
  387.         @type when: number
  388.         @return: If I am a X{If-Modified-Since} conditional request and
  389.             the time given is not newer than the condition, I return
  390.             L{http.CACHED<CACHED>} to indicate that you should write no
  391.             body.  Otherwise, I return a false value.
  392.         """
  393.  
  394.     def setETag(etag):
  395.         """Set an X{entity tag} for the outgoing response.
  396.  
  397.         That's \"entity tag\" as in the HTTP/1.1 X{ETag} header, \"used
  398.         for comparing two or more entities from the same requested
  399.         resource.\"
  400.  
  401.         If I am a conditional request, I may modify my response code
  402.         to L{NOT_MODIFIED<twisted.protocols.http.NOT_MODIFIED>} or
  403.         L{PRECONDITION_FAILED<twisted.protocols.http.PRECONDITION_FAILED>},
  404.         if appropriate for the tag given.
  405.  
  406.         @param etag: The entity tag for the resource being returned.
  407.         @type etag: string
  408.         @return: If I am a X{If-None-Match} conditional request and
  409.             the tag matches one in the request, I return
  410.             L{CACHED<twisted.protocols.http.CACHED>} to indicate that
  411.             you should write no body.  Otherwise, I return a false
  412.             value.
  413.         """
  414.  
  415.     def setHost(host, port, ssl=0):
  416.         """Change the host and port the request thinks it's using.
  417.  
  418.         This method is useful for working with reverse HTTP proxies (e.g.
  419.         both Squid and Apache's mod_proxy can do this), when the address
  420.         the HTTP client is using is different than the one we're listening on.
  421.  
  422.         For example, Apache may be listening on https://www.example.com, and then
  423.         forwarding requests to http://localhost:8080, but we don't want HTML produced
  424.         by Twisted to say 'http://localhost:8080', they should say 'https://www.example.com',
  425.         so we do:
  426.  
  427.         >>> request.setHost('www.example.com', 443, ssl=1)
  428.  
  429.         This method is experimental.
  430.         """
  431.  
  432. class ISerializable(Interface):
  433.     """DEPRECATED. Use nevow.flat.registerFlattener instead of registering
  434.     an ISerializable adapter.
  435.     """
  436.     def serialize(context):
  437.         """Serialize the adaptee, with the given context
  438.         stack if necessary.
  439.         """
  440.  
  441. class IStatusMessage(Interface):
  442.     """A marker interface, which should be set on the user's web session
  443.     to an object which can be cast to a string, which will be shown to the
  444.     user to indicate the status of the most recent operation.
  445.     """
  446.  
  447.  
  448. class IHand(Interface):
  449.     """A marker interface which indicates what object the user is currently
  450.     holding in their hand. This is conceptually the "result" of the last operation;
  451.     this interface can be used to mark a status string which indicates whether
  452.     the most recent operation completed successfully, or can be used to hold
  453.     an object which resulted from the most recent operation.
  454.     """
  455.  
  456.  
  457. class ICanHandleException(Interface):
  458.     def renderHTTP_exception(context, failure):
  459.         """Render an exception to the given request object.
  460.         """
  461.  
  462.     def renderInlineException(context, reason):
  463.         """Return stan representing the exception, to be printed in the page,
  464.         not replacing the page."""
  465.  
  466.  
  467. class ICanHandleNotFound(Interface):
  468.     def renderHTTP_notFound(context):
  469.         """Render a not found message to the given request.
  470.         """
  471.  
  472.  
  473. class IEventMaster(Interface):
  474.     pass
  475.  
  476.  
  477. class IDocFactory(Interface):
  478.     """Interface for objects that load and parse templates for Nevow's
  479.     renderers.
  480.  
  481.     The load method's context arg is optional. Loaders should be written to cope
  482.     with no context arg and either create a new context (if necessary) or raise
  483.     a ValueError if the context of the caller is important.
  484.  
  485.     If a context is passed to load() it should *not* be passed on to the
  486.     flattener/precompiler; a new context should be created if necessary. This
  487.     measure is to ensure that nothing remembered in the caller's context, i.e.
  488.     personal information in the session, leaks into the template until it is
  489.     actually rendered.
  490.     """
  491.  
  492.     def load(ctx=None, preprocessors=()):
  493.         """
  494.         Load a template and return a stan document tree.
  495.  
  496.         @param preprocessors: An iterable of one-argument callables which will
  497.         be given the stan document tree to transform before it is compiled.
  498.         """
  499.  
  500.  
  501. class ISession(Interface):
  502.     """A web session
  503.  
  504.     You can locate a Session object to represent a unique web session using
  505.     ctx.locate(ISession). This default session implementation uses cookies to
  506.     store a session identifier in the user's browser.
  507.  
  508.     TODO: Need better docs; what's a session and why and how do you use it
  509.     """
  510.  
  511.  
  512. class IRemainingSegments(Interface):
  513.     """During the URL traversal process, requesting this from the context
  514.     will result in a tuple of the segments remaining to be processed.
  515.  
  516.     Equivalent to request.postpath in twisted.web
  517.     """
  518.  
  519.  
  520. class ICurrentSegments(Interface):
  521.     """Requesting this from the context will result in a tuple of path segments
  522.     which have been consumed to reach the current Page instance during
  523.     the URL traversal process.
  524.  
  525.     Equivalent to request.prepath in twisted.web
  526.     """
  527.  
  528.  
  529. class IViewParameters(Interface):
  530.     """An interface used by url.viewhere. When this interface is remembered
  531.     above a url.viewhere embedded in a page, and the url to the current page
  532.     is rendered, this object will be consulted for additional manipulations
  533.     to perform on the url object before returning it.
  534.     """
  535.     def __iter__():
  536.         """Return an iterator which yields a series of (command, args, kw) triples,
  537.         where 'command' is a string, indicating which url method to call, 'args' is a
  538.         list indicating the arguments to be passed to this method, and 'kw' is a dict
  539.         of keyword arguments to pass to this method.
  540.         """
  541.  
  542. class II18NConfig(Interface):
  543.     """
  544.     Interface for I18N configuration.
  545.  
  546.     @ivar domain: the gettext domain
  547.  
  548.     @type domain: str
  549.  
  550.     @ivar localeDir: path to the messages files or None to use the
  551.     system default
  552.  
  553.     @type localeDir: str or None
  554.     """
  555.     domain = Attribute("The gettext domain")
  556.     localeDir = Attribute("Path to the messages files or None to use the system default")
  557.  
  558.  
  559. class ILanguages(Interface):
  560.     """
  561.     Marker interface for the sequence of strings that defines the
  562.     languages requested by the user.
  563.     """
  564.  
  565.  
  566. class ILogger(Interface):
  567.     """
  568.     An access.log writing interface.
  569.  
  570.     Remember this interface in the context stack to use alternative
  571.     logging for the resources below that point in the tree.
  572.     """
  573.     def log(ctx):
  574.         """Write a log entry for this request."""
  575.  
  576.  
  577. class IJavascriptPackage(Interface):
  578.     """
  579.     Represents information about the filesystem layout of a set of
  580.     JavaScript modules.
  581.  
  582.     @ivar mapping: A C{dict} mapping C{str} to C{str}.  The keys in
  583.     this dictionary are JavaScript module names which can be imported by
  584.     JavaScript files server by C{nevow.athena.LivePage}.  The values give
  585.     locations in the filesystem where the implementation of each module can
  586.     be found.
  587.     """
  588.  
  589.  
  590. class IAthenaTransportable(Interface):
  591.     """
  592.     An object which can be sent by Athena from the Python server to the
  593.     JavaScript client.
  594.     """
  595.     jsClass = Attribute(
  596.         """
  597.         A C{unicode} string giving the fully-qualified name of a JavaScript
  598.         function which will be invoked to unserialize the serialized form of
  599.         this object.
  600.  
  601.         The current serialization implementation is limited to supporting
  602.         values for this attribute which refer to JavaScript functions which
  603.         are defined in modules which have already been imported by the
  604.         client receiving the serialized data.  An attempt to lift this
  605.         limitation will likely be made at some future point.
  606.         """)
  607.  
  608.  
  609.     def getInitialArguments():
  610.         """
  611.         Define the arguments which will be passed to L{jsClass}.
  612.  
  613.         @rtype: L{tuple}
  614.         @return: A tuple of simple types which will be passed as positional
  615.             arguments to L{jsClass}.
  616.         """
  617.